package com.byit.controller;

import com.alibaba.fastjson.JSONObject;
import com.byit.config.UploadConfig;
import com.byit.msg.MessageResult;
import com.byit.utils.FileTools;
import com.byit.utils.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * nginx代理访问例子
 * http://localhost:8081/file/images/logo1.png
 *
 * @author wangx
 **/

@RestController
@RequestMapping("/api/upload")
public class SysUploadController {

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 头像上传
     *
     * @param file 请求对象
     * @return Object
     * @throws IOException 异常
     */
    @PostMapping("/avatars")
    public Object avatars(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            String objectName = UUID.randomUUID().toString();
            String originalFilename = file.getOriginalFilename(); // 原名称
            String suffix = FileTools.getSuffix(originalFilename);

            objectName = objectName + '.' + suffix;

            // 上传文件
            JSONObject flg = minioUtil.uploadFile(file.getInputStream(),
                    file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")), UploadConfig.FILE_IMAGE_WEB_AVATAR);


            if ("1".equals(flg.getString("flag"))) {
                return MessageResult.error("上传失败");
            } else {
                JSONObject data = new JSONObject();
                data.put("src", flg.get("path"));
                return MessageResult.success(data);
            }
        } catch (Exception e) {
            return MessageResult.error(e.getMessage());
        }
    }

    /**
     * 视频上传
     *
     * @param file 请求对象
     * @return Object
     * @throws IOException 异常
     */
    @PostMapping("/video")
    public Object video(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            String objectName = UUID.randomUUID().toString();
            String originalFilename = file.getOriginalFilename(); // 原名称
            String suffix = FileTools.getSuffix(originalFilename);

            objectName = objectName + '.' + suffix;

            // 上传文件
            JSONObject flg = minioUtil.uploadVideo(file.getInputStream(),
                    file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")), UploadConfig.FILE_IMAGE_WEB_AVATAR);


            if ("1".equals(flg.getString("flag"))) {
                return MessageResult.error("上传失败");
            } else {
                JSONObject data = new JSONObject();
                data.put("src", flg.get("path"));
                return MessageResult.success(data);
            }
        } catch (Exception e) {
            return MessageResult.error(e.getMessage());
        }
    }
}
